<?php

/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海瑞璞玉数字网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\cashier\model\order;


use app\model\BaseModel;
use app\model\order\OrderCommon;
use app\model\order\OrderRefund;

/**
 * 收银订单
 *
 * @author Administrator
 *
 */
class CashierOrderRefund extends BaseModel
{

    public function getRefundApplyData($params)
    {
        $store_id = $params[ 'store_id' ];
        $site_id = $params[ 'site_id' ];
        $order_id = $params[ 'order_id' ];
        $order_refund_model = new OrderRefund();
        $refund_array = empty($params[ 'refund_array' ]) ? [] : json_decode($params[ 'refund_array' ], true);
        if (empty($refund_array))
            return $this->error([], '请选择要退款的订单项!');

        $refund_list = [];
        foreach ($refund_array as $k => $refund_v) {
            $item = $order_refund_model->getOrderGoodsRefundInfo($refund_v, $site_id, $store_id)[ 'data' ] ?? [];
            if (!empty($item)) {
                $order_info = $item[ 'order_info' ];
                $refund_list[] = $item;
            }
        }
        if (empty($refund_list))
            return $this->error([], '请选择要退款的订单项');

        $refund_data = [];
        $refund_transfer_type = $this->getRefundTransferType($order_info);
        $refund_data[ 'refund_transfer_type' ] = $refund_transfer_type;
        $refund_data[ 'refund_list' ] = $refund_list;
        return $this->success($refund_data);

    }

    /**
     * 退款转账方式
     * @param array $params
     * @return array
     */
    public function getRefundTransferType($params = [])
    {
        $pay_type = $params[ 'pay_type' ] ?? '';
        $pay_money = $params[ 'pay_money' ] ?? 0;
        $list = array (
            '4' => [ 'name' => '现金退款', 'desc' => '与客户协商一致, 以现金的形式退款给客户' ],
            '2' => [ 'name' => '线下退款', 'desc' => '与客户协商一致, 在线下以支付宝或微信等形式退款给客户' ],
        );
//        if (in_array($pay_type, ['wechatpay', 'alipay']) && $pay_money > 0) {
        $list[ '1' ] = [ 'name' => '原路退款', 'desc' => '与客户协商一致，原路退款给客户' ];
//        }
        return $list;
    }

    public function refund($params)
    {
        //判断是不是退全部
        $order_id = $params[ 'order_id' ];
        $store_id = $params[ 'store_id' ];
        $site_id = $params[ 'site_id' ];
        $refund_transfer_type = $params[ 'refund_transfer_type' ];
        $refund_array = $params[ 'refund_array' ];
        $refund_remark = $params[ 'refund_remark' ];
        $order_common_model = new OrderCommon();
        $order_goods_ids = [];
        $order_condition = array (
            [ 'order_id', '=', $order_id ],
            [ 'store_id', '=', $store_id ]
        );
        $order_info = $order_common_model->getOrderInfo($order_condition)[ 'data' ] ?? [];
        if (empty($order_info))
            return $this->error();

        //已经退款的金额
        $order_money = $order_info[ 'order_money' ];
        $now_refund_money = $order_info[ 'refund_money' ];
        $operator = $params[ 'operator' ] ?? [];
        $surplus_money = $order_money - $now_refund_money;

        model('order_goods')->startTrans();
        try {
            $order_refund_model = new OrderRefund();
            foreach ($refund_array as $k => $v) {
                $order_goods_info = $order_common_model->getOrderGoodsInfo([ [ 'order_goods_id', '=', $k ] ])[ 'data' ] ?? [];
                $order_goods_ids[] = $k;
                //计算当前订单能退的金额(金额既不能大于剩余可退, 也不能大于当前订单项最多能退)
                $item_refund_money = $v[ 'refund_money' ];
                $goods_money = $order_goods_info[ 'real_goods_money' ];
//            $item_rate = $goods_money/$order_money;
                if ($item_refund_money > $goods_money) {
                    $item_refund_money = $goods_money;
                }
                if ($item_refund_money > $surplus_money) {
                    $item_refund_money = $surplus_money;
                }
                $data = array (
                    'order_goods_id' => $k,
                    'refund_money_type' => $refund_transfer_type,
                    'shop_refund_remark' => $refund_remark,
                    'shop_active_refund' => 1,
                    'do_refund_money' => $item_refund_money
                );
                $log_data = [
                    'uid' => $operator[ 'uid' ] ?? 0,
                    'nick_name' => $operator[ 'username' ] ?? '',
                    'action' => '门店员工【' . $operator[ 'username' ] . '】对订单进行了主动退款',
                    'action_way' => 2
                ];
                $result = $order_refund_model->orderRefundFinish($data, $operator, $log_data);
                if ($result[ 'code' ] < 0) {
                    model('order_goods')->rollback();
                    return $result;
                }

                $surplus_money -= $item_refund_money;
            }
            model('order_goods')->commit();
            return $this->success();
        } catch (\Exception $e) {
            model('order_goods')->rollback();
            return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
        }
    }
}